home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIB6.PAK / SCRIBVW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  10KB  |  334 lines

  1. // ScribVw.cpp : implementation of the CScribbleView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "ScribVw.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleView
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleView, CScrollView)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleView, CScrollView)
  31.     //{{AFX_MSG_MAP(CScribbleView)
  32.     ON_WM_LBUTTONDOWN()
  33.     ON_WM_LBUTTONUP()
  34.     ON_WM_MOUSEMOVE()
  35.     //}}AFX_MSG_MAP
  36.     // Standard printing commands
  37.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  38.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  39.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CScribbleView construction/destruction
  44.  
  45. CScribbleView::CScribbleView()
  46. {
  47.     // TODO: add construction code here
  48.  
  49. }
  50.  
  51. CScribbleView::~CScribbleView()
  52. {
  53. }
  54.  
  55. BOOL CScribbleView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     // TODO: Modify the Window class or styles here by modifying
  58.     //  the CREATESTRUCT cs
  59.  
  60.     return CView::PreCreateWindow(cs);
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CScribbleView drawing
  65.  
  66. void CScribbleView::OnDraw(CDC* pDC)
  67. {
  68.     CScribbleDoc* pDoc = GetDocument();
  69.     ASSERT_VALID(pDoc);
  70.  
  71.     // Get the invalidated rectangle of the view, or in the case
  72.     // of printing, the clipping region of the printer dc.
  73.     CRect rectClip;
  74.     CRect rectStroke;
  75.     pDC->GetClipBox(&rectClip);
  76.     pDC->LPtoDP(&rectClip);
  77.     rectClip.InflateRect(1, 1); // avoid rounding to nothing
  78.  
  79.     // Note: CScrollView::OnPaint() will have already adjusted the
  80.     // viewport origin before calling OnDraw(), to reflect the
  81.     // currently scrolled position.
  82.  
  83.     // The view delegates the drawing of individual strokes to
  84.     // CStroke::DrawStroke().
  85.     CTypedPtrList<CObList,CStroke*>& strokeList = pDoc->m_strokeList;
  86.     POSITION pos = strokeList.GetHeadPosition();
  87.     while (pos != NULL)
  88.     {
  89.         CStroke* pStroke = strokeList.GetNext(pos);
  90.         rectStroke = pStroke->GetBoundingRect();
  91.         pDC->LPtoDP(&rectStroke);
  92.         rectStroke.InflateRect(1, 1); // avoid rounding to nothing
  93.         if (!rectStroke.IntersectRect(&rectStroke, &rectClip))
  94.             continue;
  95.         pStroke->DrawStroke(pDC);
  96.     }
  97. }
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CScribbleView printing
  101.  
  102. BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
  103. {
  104.     pInfo->SetMaxPage(2);   // the document is two pages long:
  105.                             // the first page is the title page
  106.                             // the second is the drawing
  107.     BOOL bRet = DoPreparePrinting(pInfo);    // default preparation
  108.     pInfo->m_nNumPreviewPages = 2;  // Preview 2 pages at a time
  109.     // Set this value after calling DoPreparePrinting to override
  110.     // value read from .INI file
  111.     return bRet;
  112. }
  113.  
  114. void CScribbleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  115. {
  116.     // TODO: add extra initialization before printing
  117. }
  118.  
  119. void CScribbleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  120. {
  121.     // TODO: add cleanup after printing
  122. }
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // CScribbleView diagnostics
  126.  
  127. #ifdef _DEBUG
  128. void CScribbleView::AssertValid() const
  129. {
  130.     CScrollView::AssertValid();
  131. }
  132.  
  133. void CScribbleView::Dump(CDumpContext& dc) const
  134. {
  135.     CScrollView::Dump(dc);
  136. }
  137.  
  138. CScribbleDoc* CScribbleView::GetDocument() // non-debug version is inline
  139. {
  140.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScribbleDoc)));
  141.     return (CScribbleDoc*)m_pDocument;
  142. }
  143. #endif //_DEBUG
  144.  
  145. /////////////////////////////////////////////////////////////////////////////
  146. // CScribbleView message handlers
  147.  
  148. void CScribbleView::OnLButtonDown(UINT, CPoint point) 
  149. {
  150.     // Pressing the mouse button in the view window starts a new stroke
  151.  
  152.     // CScrollView changes the viewport origin and mapping mode.
  153.     // It's necessary to convert the point from device coordinates
  154.     // to logical coordinates, such as are stored in the document.
  155.     CClientDC dc(this);
  156.     OnPrepareDC(&dc);
  157.     dc.DPtoLP(&point);
  158.  
  159.     m_pStrokeCur = GetDocument()->NewStroke();
  160.     // Add first point to the new stroke
  161.     m_pStrokeCur->m_pointArray.Add(point);
  162.  
  163.     SetCapture();       // Capture the mouse until button up.
  164.     m_ptPrev = point;   // Serves as the MoveTo() anchor point for the
  165.                         // LineTo() the next point, as the user drags the
  166.                         // mouse.
  167.  
  168.     return;
  169. }
  170.  
  171. void CScribbleView::OnLButtonUp(UINT, CPoint point) 
  172. {
  173.     // Mouse button up is interesting in the Scribble application
  174.     // only if the user is currently drawing a new stroke by dragging
  175.     // the captured mouse.
  176.  
  177.     if (GetCapture() != this)
  178.         return; // If this window (view) didn't capture the mouse,
  179.                 // then the user isn't drawing in this window.
  180.  
  181.     CScribbleDoc* pDoc = GetDocument();
  182.  
  183.     CClientDC dc(this);
  184.  
  185.     // CScrollView changes the viewport origin and mapping mode.
  186.     // It's necessary to convert the point from device coordinates
  187.     // to logical coordinates, such as are stored in the document.
  188.     OnPrepareDC(&dc);  // set up mapping mode and viewport origin
  189.     dc.DPtoLP(&point);
  190.  
  191.     CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());
  192.     dc.MoveTo(m_ptPrev);
  193.     dc.LineTo(point);
  194.     dc.SelectObject(pOldPen);
  195.     m_pStrokeCur->m_pointArray.Add(point);
  196.  
  197.     // Tell the stroke item that we're done adding points to it.
  198.     // This is so it can finish computing its bounding rectangle.
  199.     m_pStrokeCur->FinishStroke();
  200.  
  201.     // Tell the other views that this stroke has been added
  202.     // so that they can invalidate this stroke's area in their
  203.     // client area.
  204.     pDoc->UpdateAllViews(this, 0L, m_pStrokeCur);
  205.  
  206.     ReleaseCapture();   // Release the mouse capture established at
  207.                         // the beginning of the mouse drag.
  208.     return;
  209. }
  210.  
  211. void CScribbleView::OnMouseMove(UINT, CPoint point) 
  212. {
  213.     // Mouse movement is interesting in the Scribble application
  214.     // only if the user is currently drawing a new stroke by dragging
  215.     // the captured mouse.
  216.  
  217.     if (GetCapture() != this)
  218.         return; // If this window (view) didn't capture the mouse,
  219.                 // then the user isn't drawing in this window.
  220.  
  221.     CClientDC dc(this);
  222.     // CScrollView changes the viewport origin and mapping mode.
  223.     // It's necessary to convert the point from device coordinates
  224.     // to logical coordinates, such as are stored in the document.
  225.     OnPrepareDC(&dc);
  226.     dc.DPtoLP(&point);
  227.  
  228.     m_pStrokeCur->m_pointArray.Add(point);
  229.  
  230.     // Draw a line from the previous detected point in the mouse
  231.     // drag to the current point.
  232.     CPen* pOldPen = dc.SelectObject(GetDocument()->GetCurrentPen());
  233.     dc.MoveTo(m_ptPrev);
  234.     dc.LineTo(point);
  235.     dc.SelectObject(pOldPen);
  236.     m_ptPrev = point;
  237.     return;
  238. }
  239.  
  240. void CScribbleView::OnUpdate(CView* /* pSender */, LPARAM /* lHint */, 
  241.     CObject* pHint) 
  242. {
  243.     // The document has informed this view that some data has changed.
  244.  
  245.     if (pHint != NULL)
  246.     {
  247.         if (pHint->IsKindOf(RUNTIME_CLASS(CStroke)))
  248.         {
  249.             // The hint is that a stroke as been added (or changed).
  250.             // So, invalidate its rectangle.
  251.             CStroke* pStroke = (CStroke*)pHint;
  252.             CClientDC dc(this);
  253.             OnPrepareDC(&dc);
  254.             CRect rectInvalid = pStroke->GetBoundingRect();
  255.             dc.LPtoDP(&rectInvalid);
  256.             InvalidateRect(&rectInvalid);
  257.             return;
  258.         }
  259.     }
  260.     // We can't interpret the hint, so assume that anything might
  261.     // have been updated.
  262.     Invalidate(TRUE);
  263.     return;
  264. }
  265.  
  266. void CScribbleView::OnInitialUpdate() 
  267. {
  268.     SetScrollSizes(MM_LOENGLISH, GetDocument()->GetDocSize());
  269.     CScrollView::OnInitialUpdate();
  270. }
  271.  
  272. void CScribbleView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
  273. {
  274.     if (pInfo->m_nCurPage == 1)  // page no. 1 is the title page
  275.     {
  276.         PrintTitlePage(pDC, pInfo);
  277.         return; // nothing else to print on page 1 but the page title
  278.     }
  279.     CString strHeader = GetDocument()->GetTitle();
  280.  
  281.     PrintPageHeader(pDC, pInfo, strHeader);
  282.     // PrintPageHeader() subtracts out from the pInfo->m_rectDraw the
  283.     // amount of the page used for the header.
  284.  
  285.     pDC->SetWindowOrg(pInfo->m_rectDraw.left,-pInfo->m_rectDraw.top);
  286.  
  287.     // Now print the rest of the page
  288.     OnDraw(pDC);
  289. }
  290.  
  291. void CScribbleView::PrintTitlePage(CDC* pDC, CPrintInfo* pInfo)
  292. {
  293.     // Prepare a font size for displaying the file name
  294.     LOGFONT logFont;
  295.     memset(&logFont, 0, sizeof(LOGFONT));
  296.     logFont.lfHeight = 75;  //  3/4th inch high in MM_LOENGLISH
  297.                             // (1/100th inch)
  298.     CFont font;
  299.     CFont* pOldFont = NULL;
  300.     if (font.CreateFontIndirect(&logFont))
  301.         pOldFont = pDC->SelectObject(&font);
  302.  
  303.     // Get the file name, to be displayed on title page
  304.     CString strPageTitle = GetDocument()->GetTitle();
  305.  
  306.     // Display the file name 1 inch below top of the page,
  307.     // centered horizontally
  308.     pDC->SetTextAlign(TA_CENTER);
  309.     pDC->TextOut(pInfo->m_rectDraw.right/2, -100, strPageTitle);
  310.  
  311.     if (pOldFont != NULL)
  312.         pDC->SelectObject(pOldFont);
  313. }
  314.  
  315. void CScribbleView::PrintPageHeader(CDC* pDC, CPrintInfo* pInfo,
  316.     CString& strHeader)
  317. {
  318.     // Print a page header consisting of the name of
  319.     // the document and a horizontal line
  320.     pDC->SetTextAlign(TA_LEFT);
  321.     pDC->TextOut(0,-25, strHeader);  // 1/4 inch down
  322.  
  323.     // Draw a line across the page, below the header
  324.     TEXTMETRIC textMetric;
  325.     pDC->GetTextMetrics(&textMetric);
  326.     int y = -35 - textMetric.tmHeight;          // line 1/10th inch below text
  327.     pDC->MoveTo(0, y);                          // from left margin
  328.     pDC->LineTo(pInfo->m_rectDraw.right, y);    // to right margin
  329.  
  330.     // Subtract out from the drawing rectange the space used by the header.
  331.     y -= 25;    // space 1/4 inch below (top of) line
  332.     pInfo->m_rectDraw.top += y;
  333. }
  334.